/**
 * 2018年12月25日下午5:55:59
 * @author 谢进财
 */
package com.baihang.platform.bean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import com.baihang.framework.dao.Record;
import com.baihang.framework.kit.PropKit;
import com.baihang.framework.kit.database.RedisKit;
import com.baihang.project.ds.dao.SystemConfigDao;

import redis.clients.jedis.Jedis;

/**
 * @author whati
 *
 */
@Component
public class StartupListenerBean
		implements ApplicationContextAware, ApplicationListener<ContextRefreshedEvent>, InitializingBean, ServletContextAware {
	private static Logger logger = LoggerFactory.getLogger(StartupListenerBean.class);
	@Autowired
	private SystemConfigDao dao;
	/* (non-Javadoc)
	 * @see org.springframework.web.context.ServletContextAware#setServletContext(javax.servlet.ServletContext)
	 */
	@Override //执行顺序2
	public void setServletContext(ServletContext arg0) {
		System.err.println("1");

	}

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	@Override //执行顺序3
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
		System.err.println("2");
	}

	/* (non-Javadoc)
	 * @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
	 */
	@Override //最先执行
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		PropKit.use("application.properties");//读取配置文件
		List<Record> list = dao.queryForRecordList("select * from sys_config");
		/*RedisKit.HOST = PropKit.get("redisHOST");
		logger.info("Host{}",PropKit.get("redisHOST"));
		RedisKit.PORT = Integer.valueOf(PropKit.get("redisPORT"));
		logger.info("PORT{}",PropKit.get("redisPORT"));
		RedisKit.PWD =null;// PropKit.get("redisPWD");
*/		//logger.info("PWD{}",PropKit.get("redisPWD"));
		Jedis jedis = RedisKit.getJedis();
		if(jedis==null) {
			logger.info("redis连不上 ");
		}
		Map<String,String> configMap = new HashMap<String,String>();
		for(Record rec:list) {			
				configMap.put(rec.getStr("Name"), rec.getStr("Value"));			
		}	
		jedis.hmset("systemConfig", configMap);		
		
	}

	/* (non-Javadoc)
	 * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
	 */
	@Override  //最后执行 还会执行2次
	public void onApplicationEvent(ContextRefreshedEvent arg0) {
		// TODO Auto-generated method stub
		System.err.println("4");
	}

}
